const service = require('../../utils/service.js')
const api = require('../../utils/util.js')
Page({
  data: {
    userInfo: {},
    shopInfo: {},
    shopData: {},
    page: 1,
    hasMoreData: true,
    goodsList: [],
    type: 1,
    classfiyList: [],
    classfiy_id: 0,
    idx: -1,
    showPon: false,
    showMyModel: false,
    classfiyName: '',
    searchVal: '',
    keyword: ''
  },

  onLoad: function (options) {
    let userInfo = wx.getStorageSync('userInfo')
    this.setData({
      userInfo: userInfo
    })
  },

  onShow: function () {
    this.init();
  },

  init: function () {
    let that = this
    this.getShopInfo(function () {
      that.getGoodsList()
      that.getAllClassfiy()
    })

    this.setData({
      goodsList: [],
      page: 1,
      hasMoreData: true
    })
  },

  // 获取店铺信息
  getShopInfo: function (cb) {
    let that = this
    wx.getStorage({
      key: 'shopInfo',
      success: function(res) {
        that.setData({
          shopInfo: res.data,
          shopData: wx.getStorageSync('shop_data_shop')
        })
        cb()
      },
    })
  },

  // 获取商品列表
  getGoodsList: function () {
    let classfiy_id = this.data.classfiy_id
    if (classfiy_id == 0) {
      classfiy_id = undefined
    }
    let that = this
    api.ajax(service.url('productListGet'), {
      unionid: this.data.shopInfo.unionid,
      token: this.data.shopData.token,
      token_key: this.data.shopData.token_key,
      shop_code: this.data.shopInfo.shop_code,
      on_sale: this.data.type,
      page: this.data.page,
      category_id: classfiy_id,
      keyword: this.data.keyword
    }, function (res) {
      if (res.code == 0) {
        if (res.data.length > 0) {
          res.data.forEach(function (item, index) {
            that.data.goodsList.push(item)
          })
          that.setData({
            hasMoreData: true,
            goodsList: that.data.goodsList
          })
          console.log(that.data.goodsList)
        } else {
          that.setData({
            hasMoreData: false
          })
        }
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      } else {
        api.toast(res.message)
      }
    })
  },

  // 获取所有商品分类
  getAllClassfiy: function () {
    let that = this
    api.ajax(service.url('categoryGetAll'), {
      unionid: this.data.shopInfo.unionid,
      token: this.data.shopData.token,
      token_key: this.data.shopData.token_key,
      shop_code: this.data.shopInfo.shop_code,
    }, function (res) {
      if (res.code == 0) {
        that.setData({
          classfiyList: res.data
        })
      } else {
        api.toast(res.message)
      }
    })
  },

  // 切换导航
  switchNav: function (e) {
    let type = e.currentTarget.dataset.type
    this.setData({
      type: type,
      goodsList: [],
      page: 1
    })
    if (type != 0) {
      this.setData({
        showPon: false,
      })
    }
    this.getGoodsList()
  },

  // 获取搜索框内容
  getSearch: function (e) {
    let val = e.detail.value
    this.setData({
      searchVal: val
    })
  },

  // 设置搜索内容
  submitSearch: function () {
    this.setData({
      keyword: this.data.searchVal,
      page: 1,
      goodsList: []
    })
    this.getGoodsList()
  },

  // 筛选
  selectCalss: function (e) {
    let classfiy_id = e.currentTarget.dataset.id
    let idx = e.currentTarget.dataset.idx
    this.setData({
      idx: idx,
      page: 1,
      goodsList: []
    })

    this.setData({
      showPon: false,
      classfiy_id: classfiy_id
    })
    this.getGoodsList()
  },

  //前往编辑商品页面
  goGoodsEdit: function(e){
    let goods_id = e.currentTarget.dataset.goodsId
    wx.navigateTo({
      url: `../shop_add/shop_add?goods_id=${goods_id}`,
    })
  },

  // 预览
  toGoodsDetail: function (e) {
    let goods_id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `../goods_detail/goods_detail?goods_id=${goods_id}&is_seller=1`,
    })
  },

  // 删除
  delGoods: function (e) {
    let that = this
    let goods_id = e.currentTarget.dataset.id
    wx.showModal({
      title: '提示',
      content: '确定删除该商品？',
      success: (rel) => {
        if (rel.confirm) {
          api.ajax(service.url('productGoodsDel'), {
            unionid: that.data.shopInfo.unionid,
            token: that.data.shopData.token,
            token_key: that.data.shopData.token_key,
            shop_code: that.data.shopInfo.shop_code,
            goods_id: goods_id
          }, function (res) {
            if (res.code == 0) {
              api.toast('删除成功')
              that.setData({
                page: 1,
                goodsList: []
              })
              that.getGoodsList()
            } else {
              api.toast(res.message)
            }
          })
        }
      }
    })
  },

  // 上架
  onTheSell: function (e) {
    let that = this
    let goods_id = e.currentTarget.dataset.id
    wx.showModal({
      title: '提示',
      content: '确定上架该商品?',
      success: (rel) => {
        if (rel.confirm) {
          api.ajax(service.url('productGoodsOnSale'), {
            unionid: that.data.shopInfo.unionid,
            token: that.data.shopData.token,
            token_key: that.data.shopData.token_key,
            shop_code: that.data.shopInfo.shop_code,
            goods_id: goods_id,
            on_sale: 1
          }, function (res) {
            if (res.code == 0) {
              api.toast('上架成功')
              that.setData({
                page: 1,
                goodsList: []
              })
              that.getGoodsList()
            } else {
              api.toast(res.message)
            }
          })
        }
      }
    })
  },

  // 添加商品
  addGoods: function () {
    wx.navigateTo({
      url: '../shop_add/shop_add',
    })
  },

  // 批量管理
  banchGoods: function () {
    let type = this.data.type
    wx.navigateTo({
      url: `../bacth_handle/bacth_handle?type=${type}`,
    })
  },

  // 分类管理
  toClassfiyHandle: function () {
    wx.navigateTo({
      url: '../classfiy_handle/classfiy_handle',
    })
  },

  // 新建分类
  addClassfiy: function () {
    this.setData({
      showMyModel: true,
      classfiyName: ''
    })
  },

  // 确定新建分类
  sureAddClassfiy: function () {
    let that = this
    let val = this.data.classfiyName
    if (val == '') {
      api.toast('请输入分类名称')
      return
    }
    api.ajax(service.url('categoryAdd'), {
      unionid: this.data.shopInfo.unionid,
      token: this.data.shopData.token,
      token_key: this.data.shopData.token_key,
      name: val,
      pid: 0,
      shop_code: this.data.shopInfo.shop_code
    }, (res) => {
      if (res.code == 0) {
        this.setData({
          showMyModel: false
        })
        api.toast('新建分类成功')
        this.getAllClassfiy()
      } else {
        api.toast(res.message)
      }
    })
  },

  showClassfiy: function () {
    this.setData({
      showPon: true
    })
  },

  // 获取分类名称
  getClassfiyName: function (e) {
    let val = e.detail.value
    this.setData({
      classfiyName: val
    })
  },

  hideMyModel: function () {
    this.setData({
      showMyModel: false
    })
  },
  hidePon: function () {
    this.setData({
      showPon: false
    })
  },

  onPullDownRefresh: function () {
    this.setData({
      page: 1,
      keyword: this.data.searchVal,
      goodsList: []
    })
    this.getGoodsList()
  },

  onReachBottom: function () {
    if (this.data.hasMoreData) {
      let page = this.data.page += 1
      this.setData({
        page: page
      })
      this.getGoodsList()
    } else {
      api.toast('没有更多数据了')
    }
  },

  onShareAppMessage: function (e) {
    let that = this
    if (e.from == 'button') {
      let goods_id = e.target.dataset.id
      let arr = this.data.goodsList
      let obj = {}
      for (var i = 0, len = arr.length; i < len; i++) {
        if (arr[i].goods_id == goods_id) {
          obj = arr[i]
        }
      }

      return {
        title: obj.name,
        path: `pages/goods_detail/goods_detail?goods_id=${goods_id}`,
        imageUrl: obj.cover
      }
    }
  },
  myCatchTouch: function () {
    return
  }
})